using System;
using System.Collections.Generic;

namespace Pitoshka.Tools.Helpers
{
	public class PagerHelper
	{
		private int _ItemsCount;
		public int ItemsCount
		{
			get { return _ItemsCount; }
			set { _ItemsCount = value; }
		}

		private int _PageSize;
		public int PageSize
		{
			get { return _PageSize; }
			set { _PageSize = value; }
		}

		private int _CurrentPage;
		public int CurrentPage
		{
			get 
			{
				if (_CurrentPage > this.TotalPageCount)
				{
					_CurrentPage = this.TotalPageCount;
				}
				return _CurrentPage; 
			}
			set { _CurrentPage = value; }
		}

		private int _VisiblePageCount;
		public int VisiblePageCount
		{
			get { return _VisiblePageCount; }
			set { _VisiblePageCount = value; }
		}


		public int StartIndex
		{
			get { return (CurrentPage - 1) * PageSize; }
		}
		public int EndIndex
		{
			get
			{
				int result = StartIndex + PageSize - 1;
				if (result > ItemsCount - 1)
					result = ItemsCount - 1;

				return result;
			}
		}
		public int ItemsAtCurrentPage
		{
			get { return EndIndex - StartIndex + 1; }
		}

		public int TotalPageCount
		{
			get
			{
				int result = ItemsCount / PageSize;
				if (ItemsCount % PageSize > 0)
					result++;

				return result;
			}
		}
		public int StartPage
		{
			get
			{
				return
					((CurrentPage - 1) / VisiblePageCount) * VisiblePageCount + 1;
			}
		}
		public int EndPage
		{
			get
			{
				int result = StartPage + VisiblePageCount - 1;
				if (result > TotalPageCount)
					result = TotalPageCount;
				return result;
			}
		}

		public int? NextPage
		{
			get
			{
				if (CurrentPage != TotalPageCount)
				{
					return CurrentPage + 1;
				}
				else
				{
					return null;
				}
			}
		}
		public int? PreviousPage
		{
			get
			{
				if (CurrentPage != 1)
				{
					return CurrentPage - 1;
				}
				else
				{
					return null;
				}
			}
		}

		public int? NextTenPage
		{
			get
			{
				if (EndPage != TotalPageCount)
				{
					return EndPage + 1;
				}
				else
				{
					return null;
				}
			}
		}

		public int? PreviousTenPage
		{
			get
			{
				if (StartPage != 1)
				{
					return StartPage - 1;
				}
				else
				{
					return null;
				}
			}
		}

		public List<int> Pages
		{
			get
			{
				List<int> result = new List<int>();
				for (int i = StartPage; i <= EndPage; i++)
				{
					result.Add(i);
				}

				return result;
			}
		}

		public PagerHelper(int ItemsCount, int PageSize, int CurrentPage)
			: this(ItemsCount, PageSize, CurrentPage, 10)
		{
		}
		public PagerHelper(int ItemsCount, int PageSize, int CurrentPage, int VisiblePageCount)
		{
			this.ItemsCount = ItemsCount;
			this.PageSize = PageSize;
			this.CurrentPage = CurrentPage;
			this.VisiblePageCount = VisiblePageCount;
		}
	}
}
